//
//  CZWelcomeViewController.swift
//  czbkiosweibo5
//
//  Created by zhangping on 15/10/14.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelcomeViewController: UIViewController {
    
    // MARK: - 属性
    /// 头像底部约束
    private var iconViewBottomCons: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()

        // 准备UI
        prepareUI()
        
        // 如果有用户头像,显示用户的头像
        if let avatar = CZUserAccount.loadAccount()?.avatar_large {
            let url = NSURL(string: avatar)
            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    // MARK: - 动画
    // 动画在界面显示出来,我们看的到的时候再做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    
    /// 头像从下方移动到上方
    private func moveAnimation() {
        // 当使用自动约束时,不要去修改view的frame bounds center, 修改约束
        iconViewBottomCons?.constant = -UIScreen.mainScreen().bounds.height - iconViewBottomCons!.constant
        
        // 动画, 使用弹簧效果
        // usingSpringWithDamping: 范围 0 - 1之间, 数值越小弹簧效果月明显
        // initialSpringVelocity: 初始速度.
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 移动动画完成, 是label.alpha = 1
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // TODO: 回到首页
//                        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(true)
                        AppDelegate.switchViewController(true)
                })
        }
    }
    
    // MARK: - 准备UI
    /// 准备UI
    private func prepareUI() {
        // 添加子控件
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        // 添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景   填充父控件   VFL
        backgroundImageView.ff_Fill(view)
        // constraintsWithVisualFormat返回的是约束的数组
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg" : backgroundImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg" : backgroundImageView]))
        
        // 头像
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        
        // 获取约束
        // 获取哪个view上的约束: iconView
        // constraintsList: iconView上的所有约束
        // attribute: 要获取的约束
        iconViewBottomCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        // X 方向 CenterX 和父控件 CenterX 重合
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        // Y 方向 底部 距离 父控件底部 160
//        let bottomCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(bottomCons)
//        iconViewBottomCons = bottomCons
//        
//        // 宽度
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        
//        // 高度
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // 欢迎label
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
        
        // X 方向 CenterX 和iconView CenterX 重合
//        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        // Y 方向 顶部 距离 iconView 底部 16
//        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }

    // MARK: - 懒加载控件
    /// 背景
    private lazy var backgroundImageView: UIImageView = {
        // 创建imageView
        let image = UIImage(named: "ad_background")
        let imageView = UIImageView(image: image)
        
        return imageView
    }()
    
    /// 头像
    private lazy var iconView: UIImageView = {
        // 创建imageView
        let image = UIImage(named: "avatar_default_big")
        let imageView = UIImageView(image: image)
        
        // 设置圆角 170 / 2 = 85, 85 / 2 = 42.5
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 欢迎归来label
    private lazy var messageLabel: UILabel = {
        // 创建 label
        let label = UILabel()
        label.text = "欢迎归来"
        
        // 设置label适应文本尺寸
        label.sizeToFit()
        
        // 透明
        label.alpha = 0
        
        return label
    }()
}
